package cloud.maque.admin.impl;

import cloud.maque.admin.domain.entity.MqPrivilege;
import cloud.maque.admin.domain.entity.MqPrivilegeGroup;
import cloud.maque.admin.domain.vo.MqPrivilegeGroupVo;
import cloud.maque.admin.mapper.MqPrivilegeGroupMapper;
import cloud.maque.admin.service.MqPrivilegeGroupService;
import cloud.maque.admin.service.MqPrivilegeService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 4color
 * @description 权限组
 * @date 2023-03-17
 */
@Service
public class MqPrivilegeGroupServiceImpl extends ServiceImpl<MqPrivilegeGroupMapper, MqPrivilegeGroup> implements MqPrivilegeGroupService {

    @Autowired
    MqPrivilegeService privilegeService;

    @Override
    public MqPrivilegeGroup saveGroup(MqPrivilegeGroup privilegeGroup) {
        Assert.hasText(privilegeGroup.getGroupName(), "组名不能为空");
        saveOrUpdate(privilegeGroup);
        return privilegeGroup;
    }

    @Override
    public Boolean delete(String groupId, String tenantId) {

        if (privilegeService.countByGroupId(groupId) > 0) {
            throw new RuntimeException("组里还有权限，不能删除");
        }

        QueryWrapper<MqPrivilegeGroup> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqPrivilegeGroup::getTenantId, tenantId);
        wrapper.lambda().eq(MqPrivilegeGroup::getPrivilegeGroupId, groupId);

        return remove(wrapper);
    }

    @Override
    public List<MqPrivilegeGroupVo> Tree(PaginateDataResquest<MqPrivilegeGroup> pageReq) {


        QueryWrapper<MqPrivilegeGroup> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getGroupName())) {
            wrapper.lambda().like(MqPrivilegeGroup::getGroupName, pageReq.getData().getGroupName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getParentGroupId())) {
            wrapper.lambda().eq(MqPrivilegeGroup::getParentGroupId, pageReq.getData().getParentGroupId());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
            wrapper.lambda().eq(MqPrivilegeGroup::getTenantId, pageReq.getData().getTenantId());
        }

        wrapper.lambda().orderByAsc(MqPrivilegeGroup::getGroupOrder);
        wrapper.lambda().orderByAsc(MqPrivilegeGroup::getPrivilegeGroupId);
        List<MqPrivilegeGroup> lists = this.baseMapper.selectList(wrapper);

        List<MqPrivilegeGroupVo> voList = new ArrayList<>();

        if (lists != null) {

            //取得所有自定义权限
            MqPrivilege mq = new MqPrivilege();
            mq.setTenantId(pageReq.getData().getTenantId());
            List<MqPrivilege> privilegeList = privilegeService.allList(mq);

            lists.forEach(group -> {
                MqPrivilegeGroupVo vo = new MqPrivilegeGroupVo();
                vo.setId(group.getPrivilegeGroupId());
                vo.setName(group.getGroupName());
                vo.setSort(group.getGroupOrder());
                vo.setType("group");


                if (privilegeList != null) {

                    List<MqPrivilegeGroupVo> find = new ArrayList<>();
                    privilegeList.forEach(item -> {

                        if (item.getPrivilegeGroupId().equals(vo.getId())) {
                            MqPrivilegeGroupVo voSub = new MqPrivilegeGroupVo();
                            voSub.setId(item.getPrivilegeId());
                            voSub.setSort(item.getPrivilegeOrder());
                            voSub.setName(item.getPrivilegeName());
                            voSub.setValue(item.getPrivilegeCode());
                            voSub.setType("item");
                            find.add(voSub);
                        }

                    });

                    vo.setChildren(find);

                }

                voList.add(vo);


            });


        }

        return voList;
    }

    @Override
    public List<MqPrivilegeGroup> List(MqPrivilegeGroup pageReq) {


        QueryWrapper<MqPrivilegeGroup> wrapper = new QueryWrapper<>();

        if (!StrUtil.isEmptyIfStr(pageReq.getGroupName())) {
            wrapper.lambda().like(MqPrivilegeGroup::getGroupName, pageReq.getGroupName());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getParentGroupId())) {
            wrapper.lambda().eq(MqPrivilegeGroup::getParentGroupId, pageReq.getParentGroupId());
        }

        if (!StrUtil.isEmptyIfStr(pageReq.getTenantId())) {
            wrapper.lambda().eq(MqPrivilegeGroup::getTenantId, pageReq.getTenantId());
        }

        wrapper.lambda().orderByAsc(MqPrivilegeGroup::getGroupOrder);
        wrapper.lambda().orderByAsc(MqPrivilegeGroup::getPrivilegeGroupId);
        List<MqPrivilegeGroup> lists = this.baseMapper.selectList(wrapper);


        return lists;
    }
}
